package com.naiterui.ehp.bs.emr.controller.openapi;

import cn.hutool.json.JSONUtil;
import com.naiterui.ehp.bp.constants.SwaggerTags;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.utils.encypt.DESUtil;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;
import com.naiterui.ehp.bs.emr.service.IHisService;
import com.naiterui.ehp.bs.emr.vo.openapi.his.AnalyzeRecordVO;
import com.naiterui.ehp.bs.emr.vo.openapi.his.CaseRecordVO;
import com.naiterui.ehp.bs.emr.vo.openapi.his.ExamineRecordVO;
import com.naiterui.ehp.bs.emr.vo.openapi.his.HisParamVO;
import com.naiterui.ehp.bs.emr.vo.openapi.his.MedicalRecordVO;
import com.naiterui.ehp.bs.emr.vo.openapi.his.PrescriptionRecordVO;
import com.naiterui.ehp.bs.emr.vo.openapi.his.SignParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.Collections;
import java.util.List;

/**
 * @author yiren
 * @since 2022/1/24
 */
@Slf4j
@Api(value = "实体医疗就诊记录相关", tags = SwaggerTags.OPENAPI_EMR)
@Validated
@RequestMapping("emr/openapi/his")
@RestController
@RequiredArgsConstructor
public class OpenHisController {

	private final IHisService hisService;


	@ApiOperation(value = "查询线下就诊记录", notes = "查询线下就诊记录", tags = SwaggerTags.OPENAPI_EMR)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "key", value = "身份证号加密串", required = true, dataType = "string", paramType = "query"),})
	@GetMapping("medical/record")
	public ResponseVO<List<MedicalRecordVO>> findMedicalRecord(@NotEmpty(message = "key不能为空") String key) throws BusinessException {
		log.info("查询线下就诊记录 key:{}", key);
		SignParam signParam = this.getSignParam(key);
		return PublicService.returnResponseVO(hisService.findMedicalRecord(signParam.getIdCard(), signParam.getName()));
	}

	@ApiOperation(value = "获取病历记录", notes = "获取病历记录", tags = SwaggerTags.OPENAPI_EMR)
	@GetMapping("cases/record")
	public ResponseVO<List<CaseRecordVO>> getCaseRecord(HisParamVO paramVO) throws BusinessException {
		log.info("查询线下病历记录 param:{}", JSONUtil.toJsonStr(paramVO));
		return PublicService.returnResponseVO(Collections.singletonList(hisService.getCaseRecord(paramVO.getMzh())));
	}


	@ApiOperation(value = "获取处方记录", notes = "获取处方记录", tags = SwaggerTags.OPENAPI_EMR)
	@GetMapping("prescription/record")
	public ResponseVO<List<PrescriptionRecordVO>> findPrescriptionRecord(HisParamVO paramVO) throws BusinessException {
		log.info("查询线下处方记录 param:{}", JSONUtil.toJsonStr(paramVO));
		return PublicService.returnResponseVO(hisService.findPrescriptionRecord(paramVO.getMzh()));
	}

	@ApiOperation(value = "获取化验记录", notes = "获取化验记录", tags = SwaggerTags.OPENAPI_EMR)
	@GetMapping("analyze/record")
	public ResponseVO<List<AnalyzeRecordVO>> findAnalyzeRecord(HisParamVO paramVO) throws BusinessException {
		log.info("查询线下化验记录 param:{}", JSONUtil.toJsonStr(paramVO));
		return PublicService.returnResponseVO(hisService.findAnalyzeRecord(paramVO.getMzh()));
	}

	@ApiOperation(value = "获取检查记录", notes = "获取检查记录", tags = SwaggerTags.OPENAPI_EMR)
	@GetMapping("examine/record")
	public ResponseVO<List<ExamineRecordVO>> findExamineRecord(HisParamVO paramVO) throws BusinessException {
		log.info("查询线下检查记录 param:{}", JSONUtil.toJsonStr(paramVO));
		return PublicService.returnResponseVO(Collections.singletonList(hisService.findExamineRecord(paramVO.getMzh())));
	}



	/**
	 * 解析患者身份证号
	 *
	 * @param key
	 * @return
	 * @throws BusinessException
	 */
	public SignParam getSignParam(String key) throws BusinessException {
		String secret = ConfigUtil.getString("his.hisSecret");
		try {
			String paramStr = DESUtil.desDecrypt(secret, key);
			String[] params = paramStr.split("\\|");
			return SignParam.builder().idCard(params[0]).name(params[1]).build();
		} catch (Exception e) {
			log.error("开放接口-解密失败 key:{}，secret:{}", key, secret, e);
			throw new BusinessException(ExceptionCodes.SIGN_ERR);
		}

	}
}
